namespace SimpleWebServices.Configuration
{
    using System;
    using System.Collections.ObjectModel;
    using System.Xml;
    using System.Xml.Serialization;
    using System.Xml.XPath;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using Xunit;

    public sealed class DataSettingsFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<DataSettings>()
                .DerivesFrom<object>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("data")
                .Implements<IXmlSerializable>()
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            DataSettings actual = (
                "<data enabled='true'>" +
                    "<xml enabled='true' path='~Data.object.xml' />" +
                "</data>").Deserialize<DataSettings>();

            Assert.True(actual.Enabled.Value);
            Assert.True(actual.XmlDataSettings[0].Enabled.Value);
            Assert.Equal<string>("~Data.object.xml", actual.XmlDataSettings[0].Path);
        }

        [Fact]
        public void deserializeEmpty()
        {
            DataSettings actual = "<data />".Deserialize<DataSettings>();

            Assert.NotNull(actual);

            Assert.Empty(actual.XmlDataSettings);
        }

        [Fact]
        public void serialize()
        {
            DataSettings obj = new DataSettings
            {
                Enabled = true,
                XmlDataSettings =
                {
                    new XmlDataSetting
                    {
                        Enabled = true,
                        Path = @"~\Data"
                    }
                }
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate(@"1=count(/data[@enabled='true']/xml[@enabled='true'][@path='~\Data'])"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new DataSettings());
        }

        [Fact]
        public void prop_Enabled()
        {
            Assert.True(new PropertyExpectations(typeof(DataSettings).GetProperty("Enabled"))
                .IsAutoProperty<bool?>()
                .Result);
        }

        [Fact]
        public void prop_XmlDataSettings()
        {
            Assert.True(new PropertyExpectations(typeof(DataSettings).GetProperty("XmlDataSettings"))
                .TypeIs<Collection<XmlDataSetting>>()
                .DefaultValueIsNotNull()
                .Result);
        }

        [Fact]
        public void op_Upsert()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository tests = FakeRepository.Register<TestRecord>();

                DataSettings obj = new DataSettings
                {
                    XmlDataSettings =
                    {
                        new XmlDataSetting
                        {
                            Enabled = true,
                            Path = @"Data\Record.Test.object.xml"
                        }
                    }
                };

                obj.Upsert();

                Assert.True((bool)tests.Xml.CreateNavigator().Evaluate("2 = count(//test[@value])"));
            }
            finally
            {
                FakeRepository.Reset();
            }
        }

        [Fact]
        public void IXmlSerializable_op_GetSchema()
        {
            Assert.Throws<NotSupportedException>(() => (new DataSettings() as IXmlSerializable).GetSchema());
        }

        [Fact]
        public void IXmlSerializable_op_ReadXml_XmlReaderNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new DataSettings() as IXmlSerializable).ReadXml(null as XmlReader));
        }

        [Fact]
        public void IXmlSerializable_op_WriteXml_XmlWriterNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new DataSettings() as IXmlSerializable).WriteXml(null as XmlWriter));
        }
    }
}